package cn.disicn.controller;

import java.util.Date;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import cn.disicn.common.Const;
import cn.disicn.common.ServerResponse;
import cn.disicn.model.SecAdminUser;
import cn.disicn.util.IDUtil;
import cn.disicn.util.ImageUtil;
import cn.disicn.util.MD5Util;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;

/**
 * @author zjk
 * @date 2017年12月20日 上午10:53:08
 * @desc 后台管理员
 * 
 */
public class SecAdminUserController extends Controller {

	private static final SecAdminUser secAdminuser = new SecAdminUser();

	/**
	 * 
	 * @desc 后台登录
	 */
	@Clear
	public void login() {
		String code = getPara("code");// 验证码
		String adminName = getPara("adminName");// 用户名
		String adminPassword = getPara("adminPassword");// 密码
		String regPassword = "^[0-9a-zA-Z]{6,16}$";
		ServerResponse<SecAdminUser> response = null;
		if (code != null && !StringUtils.isBlank(code)) {
			if (secAdminuser.valiImgCode(code, getSession()).isSuccess()) {
				if (adminName != null && adminPassword != null && !StringUtils.isBlank(adminName)
						&& !StringUtils.isBlank(adminPassword) && adminPassword.matches(regPassword)) {
					response = secAdminuser.adminLogin(adminName, adminPassword);
					setSessionAttr(Const.UserType.CURRENT_ADMIN, response.getData());
					renderJson(response);
				} else {
					renderJson(ServerResponse.createByErrorMsg("账号密码错误"));
				}
			} else {
				renderJson(response);
			}
			return;
		}
		renderJson(ServerResponse.createByErrorMsg(Const.ErrorResult.PARAMETER_ERROR));
	}

	/**
	 * 
	 * @desc 获取图片验证码图片二进制文件
	 */
	@Clear
	public void getPictureVerificationCode() {
		Map<String, String> graphics = ImageUtil.getGraphics();
		setSessionAttr(Const.CacheType.IMG_CODE, graphics.get("rand"));
		renderJson(graphics);
	}

	/**
	 * 
	 * @desc 退出登录
	 */
	public void exitLogon() {
		setSessionAttr(Const.UserType.CURRENT_ADMIN, null);
		redirect("/adminLogin");
	}

	/**
	 * 
	 * @desc 添加管理员账号
	 */
	public void addAdminUser() {
		String userName = getPara("userName");
		String pwd = getPara("pwd");
		String code = getPara("code");
		String userNameReg = "/^(?!^\\d+$)[0-9a-zA-Z]{3,10}$/";
		String pwdReg = "/^(\\w){6,12}$/";
		if (!StringUtils.isBlank(userName) && !StringUtils.isBlank(pwd) && !StringUtils.isBlank(code)
				&& !userNameReg.matches(userName) && !pwdReg.matches(pwd)) {
			ServerResponse<String> response = secAdminuser.valiImgCode(code, getSession());
			if (response.isSuccess()) {// 判断验证码是否正确
				if (!secAdminuser.valiUserName(userName).isSuccess()) {
					secAdminuser.setUserName(userName);
					secAdminuser.setPassword(MD5Util.MD5EncodeUtf8(pwd));
					secAdminuser.setUpdateTime(new Date());
					secAdminuser.setId(IDUtil.getId());
					renderJson(secAdminuser.addSecAdmin(secAdminuser));
				} else {
					renderJson(ServerResponse.createByErrorMsg(Const.ErrorResult.ACCOUNT_EXIST));
				}
			} else {
				renderJson(response);
			}
			return;
		}
		renderJson(ServerResponse.createByErrorMsg(Const.ErrorResult.PARAMETER_ERROR));
	}
}